package controller;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.*;
import java.nio.charset.StandardCharsets;

@Controller
@RequestMapping("/file")
public class DownloadController {

    @Autowired
    private ServletContext application;

    @RequestMapping("/download1")
    public void download1(HttpServletResponse response) throws UnsupportedEncodingException {
        System.out.println("DownloadController.download1()");

        String downloadDir=application.getRealPath("WEB-INF/download");
        String fileName="a.png";

        response.setHeader("content-disposition",new String(("attachment;filename="+fileName).getBytes(StandardCharsets.UTF_8),"ISO8859-1"));

        try(OutputStream out=response.getOutputStream();
            InputStream in=new FileInputStream(new File(downloadDir,fileName))){


            byte[] b=new byte[2048];

            int n;

            while((n=in.read(b))!=-1){
                out.write(b,0,n);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }

//    @RequestMapping("/download2")
//    public ResponseEntity<String> download2(){
//        System.out.println("DownloadController.download2()");
//        return new ResponseEntity<String>("<h1>Hello</h1>", HttpStatus.OK);
//    }

//    @RequestMapping("/download2")
//    public ResponseEntity<String> download2(){
//        System.out.println("DownloadController.download2()");
//        return ResponseEntity.ok()
//                .body("<h1>Hello</h1>");
//    }

//    @RequestMapping("/download2")
//    public ResponseEntity<String> download2(){
//        System.out.println("DownloadController.download2()");
//        return ResponseEntity.status(HttpStatus.OK)
//                .body("<h1>Hello</h1>");
//    }

//    @RequestMapping("/download2")
//    public ResponseEntity<Resource> download2(){
//        System.out.println("DownloadController.download2()");
//
//        String downloadDir=application.getRealPath("WEB-INF/download");
//        String fileName="a.png";
//
//        Resource resource=new FileSystemResource(new File(downloadDir,fileName));
//
//        HttpHeaders headers=new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//        headers.setContentDispositionFormData("attachment",new String(fileName.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1));
//
//        return new ResponseEntity<Resource>(resource,headers,HttpStatus.OK);
//    }


    @RequestMapping("/download2")
    public ResponseEntity<byte[]> download2() throws IOException {
        System.out.println("DownloadController.download2()");

        String downloadDir=application.getRealPath("WEB-INF/download");
        String fileName="a.png";

        Resource resource=new FileSystemResource(new File(downloadDir,fileName));

        HttpHeaders headers=new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment",new String(fileName.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1));

        return ResponseEntity.ok()
                             .headers(headers)
                             .body(resourceToByteArray(resource));
    }

    private byte[] resourceToByteArray(Resource resource) throws IOException {
        return StreamUtils.copyToByteArray(resource.getInputStream());
    }
}























